﻿// FogBugz Community Case List Plugin
// ----------------------------------
// 
// Copyright 2010, 2011 by Sirius Technologies AG / Arsène von Wyss - avw@gmx.ch
// 
// Development has been sponsered by Sirius Technologies AG, Basel
// 
// Source:
// 
// https://communitycaselist.googlecode.com/hg/
// 
// License:
// 
// The library is distributed under the GNU Lesser General Public License:
// http://www.gnu.org/licenses/lgpl.html
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Text;

using FogCreek.FogBugz.Plugins.Api;

namespace Sirius.FogBugz {
	public class PluginPageLinkBuilder {
		public static PluginPageLinkBuilder ForBinaryPage(CPluginApi api) {
			if (api == null) {
				throw new ArgumentNullException("api");
			}
			return new PluginPageLinkBuilder(api, api.Url.PluginBinaryPageUrl());
		}

		public static PluginPageLinkBuilder ForConfigPage(CPluginApi api) {
			if (api == null) {
				throw new ArgumentNullException("api");
			}
			return new PluginPageLinkBuilder(api, api.Url.PluginConfigPageUrl());
		}

		public static PluginPageLinkBuilder ForPage(CPluginApi api) {
			if (api == null) {
				throw new ArgumentNullException("api");
			}
			return new PluginPageLinkBuilder(api, api.Url.PluginPageUrl());
		}

		public static PluginPageLinkBuilder ForRawPage(CPluginApi api) {
			if (api == null) {
				throw new ArgumentNullException("api");
			}
			return new PluginPageLinkBuilder(api, api.Url.PluginRawPageUrl());
		}

		private readonly CPluginApi api;
		private readonly StringBuilder link = new StringBuilder();

		public PluginPageLinkBuilder(CPluginApi api, string pageUrl) {
			if (api == null) {
				throw new ArgumentNullException("api");
			}
			if (string.IsNullOrEmpty(pageUrl)) {
				throw new ArgumentNullException("pageUrl");
			}
			this.api = api;
			link = new StringBuilder(pageUrl);
		}

		public PluginPageLinkBuilder Append(string name, string value) {
			if (string.IsNullOrEmpty(name)) {
				throw new ArgumentNullException("name");
			}
			link.Append('&');
			link.Append(Uri.EscapeUriString(api.AddPluginPrefix(name)));
			link.Append('=');
			if (!string.IsNullOrEmpty(value)) {
				link.Append(Uri.EscapeUriString(value));
			}
			return this;
		}

		public override string ToString() {
			return link.ToString();
		}
	}
}
